package com.stone.schedule.facade.base;

import com.stone.schedule.common.util.BeanValidator;
import com.sun.deploy.util.ArrayUtil;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import javax.validation.groups.Default;
import java.io.Serializable;

/**
 * Created by stone on 2017/8/31.
 */
@Data
@ToString
public abstract class BaseRequestDTO implements Serializable {

	private static final long serialVersionUID = -5812015914474910707L;

	public abstract void valid();

	protected void validate(Object object, Class... groups) {
		if (0 == groups.length) {
			groups = (Class[])((Class[])ArrayUtils.add(groups, Default.class));
		}
		final String errorMessage = BeanValidator.validate(object, groups);
		if(StringUtils.isNotBlank(errorMessage)){
			throw new IllegalArgumentException(errorMessage);
		}

	}

}
